<template>
  <div class="adminBox">
    <div>
      <ul class="adminUL">
        <li>
          <router-link :to="{ name: 'login' }">登入</router-link>
        </li>
        <li>
          <router-link :to="{ name: 'register' }">注册</router-link>
        </li>
      </ul>
    </div>
    <Form
      ref="formInline"
      class="formBox"
      :model="formInline"
      :rules="ruleInline"
      inline
    >
      <FormItem prop="userName" class="formItem">
        <Input type="text" v-model="formInline.userName" placeholder="Username">
          <Icon type="ios-person-outline" slot="prepend"></Icon>
        </Input>
      </FormItem>
      <FormItem prop="password" class="formItem">
        <Input
          type="password"
          v-model="formInline.password"
          placeholder="Password"
        >
          <Icon type="ios-lock-outline" slot="prepend"></Icon>
        </Input>
      </FormItem>
      <div class="formVerify formItem">
        <FormItem prop="verifyCode" class="formItem">
          <Input
            type="text"
            class="formVerify-input"
            v-model="formInline.verifyCode"
            placeholder="verifyCode"
          >
            <Icon type="ios-lock-outline" slot="prepend"></Icon>
          </Input>
        </FormItem>
        <div class="formVerify-svg" @click="getCapt" v-html="code">图形</div>
      </div>
      <FormItem>
        <Button type="primary" @click="handleSubmit"
          >Signin</Button
        >
        <router-link :to="{ name: 'forget' }">忘记密码</router-link>
      </FormItem>
    </Form>
  </div>
</template>
<script>
import { getCode,login } from "@/api/loginApi.js";
import {v4 as uuid} from 'uuid'
export default {
  data() {
    return {
      code: "",
      formInline: {
        userName: "",
        password: "",
        verifyCode: "",
      },
      ruleInline: {
        userName: [
          {
            required: true,
            message: "Please fill in the user name",
            trigger: "blur",
          },
          { type: "email", message: "Incorrect email format", trigger: "blur" },
        ],
        password: [
          {
            required: true,
            message: "Please fill in the password.",
            trigger: "blur",
          },
          {
            type: "string",
            min: 6,
            message: "The password length cannot be less than 6 bits",
            trigger: "blur",
          },
        ],
        verifyCode: [
          {
            required: true,
            message: "Please fill in the code.",
            trigger: "blur",
          },
          {
            type: "string",
            min: 6,
            max: 6,
            message: "The code length cannot be less than 6 bits",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleSubmit(name) {
      login({
        userName:this.formInline.userName,
        password:this.formInline.password,
        code:this.formInline.verifyCode,
        sid:this.$store.state.sid
      }).then(res=>{
        if (res.code===200) {
            console.log(res);
            this.formInline.userName=''
            this.formInline.password=''
            this.formInline.verifyCode=''
        }
      }).catch(err=>{
        const data = err.response.data
        if (data.code==500) {
          this.$alert('用户名密码错误,请检查')
        }else{
          this.$alert('服务器错误')
        }
      })
    },
    //验证码接口请求封装
    getCapt() {
      let sid = this.$store.state.sid
      getCode(sid).then((res) => {
        console.log(res,77);
        if (res.code == 200) {
          this.code = res.data;
        }
      });
    },
  },
  mounted() {
    let sid = ''
    if(localStorage.getItem('sid')){
      sid=localStorage.getItem('sid')
    }else{
      sid = uuid()
      localStorage.setItem('sid',sid)
    }
    this.$store.commit('setSid',sid)
    this.getCapt();
  },
};
</script>
<style lang="scss" scoped>
.adminBox {
  width: 600px;
  margin: 0 auto;
}
.formBox {
  text-align: center;
  .formItem {
    display: block;
    width: 320px;

    margin: 0 auto 28px;
  }
  .formVerify {
    display: flex;
    flex-wrap: nowrap;
    .formVerify-svg {
      width: 60%;
      vertical-align: middle;
    }
  }
}
</style>
<style lang="scss">
.formItem .ivu-input {
  height: 40px;
}
.formVerify-svg svg {
  height: 40px !important;
}
</style>